﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XbsWebAPI.Core.Repositories.Feedback;
using XbsWebAPI.EntityFrameworkCore;
using XbsWebAPI.EntityFrameworkCore.Entities;

namespace XbsWebAPI.Core.Repositories
{
    public class OrderRepository : IOrderRepository
    {
        private readonly DataContext _dataContext;

        public OrderRepository(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public IQueryable<OrderDetail> AsQueryable()
        {
            return _dataContext.OrderDetails.AsQueryable();
        }

        public OrderDetail Create(OrderDetail entity)
        {
            entity.Id = Guid.NewGuid();
            entity.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            _dataContext.OrderDetails.Add(entity);
            _dataContext.SaveChanges();
            return entity;
        }

        public Task<OrderDetail> CreateAsync(OrderDetail entity)
        {
            return Task.FromResult(Create(entity));
        }

        public OrderDetail Delete(Guid id)
        {
            //通过Id查询数据
            var entity = _dataContext.OrderDetails.FirstOrDefault(a => a.Id == id);
            if (entity == null)
                return null;
            _dataContext.OrderDetails.Remove(entity);
            _dataContext.SaveChanges();
            return entity;
        }

        public Task<OrderDetail> DeleteAsync(Guid id)
        {
            return Task.FromResult(Delete(id));
        }

        public IQueryable<OrderDetail> Query(Expression<Func<OrderDetail, bool>> filter)
        {
            throw new NotImplementedException();
        }

        public OrderDetail QueryById(Guid id)
        {
            var order = _dataContext.OrderDetails.FirstOrDefault(b => b.Id == id);
            return order;
        }

        public OrderDetail Update(OrderDetail entity)
        {
            throw new NotImplementedException();
        }

        public Task<OrderDetail> UpdateAsync(OrderDetail entity)
        {
            throw new NotImplementedException();
        }
    }
}
